package com.cskaoyan.service.wx.storage;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.mapper.MarketStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/09/11:25
 * @Description:
 */
@Service
public class WxStorageServiceImpl implements WxStorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public MarketStorage wxStorageUpload(MultipartFile myFile) throws IOException {
        MarketStorage marketStorage = new MarketStorage();

        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        Long size=myFile.getSize();
        String contentType = myFile.getContentType();
        marketStorage.setType(contentType);
        //long转换为int类型
        marketStorage.setSize(size.intValue());
        UUID uuid = UUID.randomUUID();
        String originalFilename = myFile.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        String suffix="";
        if (split!=null&&split.length>1){
            suffix=split[split.length-1];
        }
        String uuidFileName=uuid.toString().replace("-","")+"."+suffix;


        File file = new File("c:/43th_project2_picture/",uuidFileName);
        myFile.transferTo(file);
        marketStorage.setUrl("http://localhost:8083/wx/storageobject/fetch/"+uuidFileName);
        marketStorage.setName(originalFilename);
        marketStorage.setDeleted(false);
        marketStorage.setKey("wxFeedback");
        marketStorageMapper.insert(marketStorage);
        MarketStorage marketStorage1 = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        return marketStorage1;
    }
}
